<template>
    <div class="selectpp-auditor">
        <div style="text-align:center;margin-bottom:10px">
            <RadioGroup v-model="partype" type="button" size="small" @on-change="ptchange">
                <Radio label="dus">年级组</Radio>
                <Radio label="dep">学科组</Radio>
                <Radio label="all">所有人</Radio>
            </RadioGroup>
        </div>
        <Input v-model="seartxt" size="small" placeholder="搜索人员"  @on-change="toSearch(seartxt)" v-if="partype=='all'"/>
        <Select v-model="dutie_id" v-if="partype=='dus'" size="small" @on-change="chax" placeholder="请选择年级组">
            <Option :value="item.value" v-for="(item,ind) in duties" :key="ind">{{item.title}}</Option>
        </Select>
         <Select v-model="dutie_ids" v-if="partype=='dep'" size="small" @on-change="chax" placeholder="请选择学科组">
            <Option :value="item.value" v-for="(item,ind) in trees" :key="ind">{{item.title}}</Option>
        </Select>

        <div style="min-height:250px;margin-top:7px;height:400px;overflow:auto;position: relative;">
       
            <CheckboxGroup v-model="examine_user" @on-change="toselec">
                <div v-for="item in examiners" :key="item.user_id" style="margin-bottom:2px"><Checkbox :label="item.user_id">{{item.teacher_name.zh?item.teacher_name.zh:item.teacher_name.mn}}</Checkbox></div>
            </CheckboxGroup>
            <Spin size="large" fix v-if="spinShow"></Spin>
        </div>

    </div>
</template>
<script>
export default {
    data() {
        return {
            spinShow:false,
            partype: 'dus',
            dutie_id: '',
            dutie_ids: '',
            seartxt: '',
            examine_user: [],
            duties: [],
            examiners: [],
            examiners_copy: [],
            // 菜单树
            trees: []
        }
    },
    props: ['value'],
    methods: {
         /************************************************************
          * 
          * 改变上面的3个菜单；
          * 
          ************************************************************/ 
        ptchange(type) {
            if(type=='all') {
				this.examiners = this.examiners_copy
			} else if(type=='dus'){
				this.examiners = []
				this.dutie_id = ''
			} else if(type=='dep') {
                this.examiners = []
				this.dutie_ids = ''
                if(this.trees.length==0) {
                    this.loadep()
                }
            }
        },
         /************************************************************
          * 
          * 当点击学科组或年级组查找教师
          * 
          ************************************************************/ 
        chax(id) {
            if(id!=undefined){
                this.spinShow=true
                let url = `api/user/v1/get/teacher/user/list?department_id=[${id}]`
                this.$http.get(url)
                .then(val=>{   
                    this.examiners = val.data
                    this.spinShow=false          
                })
                .catch(err=>{
                    if(err.code && err.code==422) {
                        this.$Message.info(this.$t('honorschooltelst'));
                    }    
                    this.spinShow=false                       
                })

            }
        },

         /************************************************************
          * 
          * 搜索人员
          * 
          ************************************************************/ 
		toSearch(txt) {
			if(txt.length!=0) {
				this.examiners = this.examiners_copy.filter(item=> {
					return new RegExp(txt).test(item.teacher_name)
				})
			} else {
				this.examiners = this.examiners_copy
			}
        },
        
         /************************************************************
          * 
          * 加载年级组
          * 
          ************************************************************/ 
        zwload() {

              this.$http.get('api/watchlist/v1/get/departmentstest/test')
                .then(val=>{   
                    this.duties = val.data.map(item=> ({
                        value: item.id,
                        title: item.department_name.zh?item.department_name.zh:item.department_name.mn
                    }))         
                })
                .catch(err=>{
                    if(err.code && err.code==422) {
                        this.$Message.info(this.$t('gaoyannjz'));
                    }                          
                })
        },
         /************************************************************
          * 
          * 加载全部人员
          * 
          ************************************************************/ 
        getExamine(){
            this.$http.get('api/user/v1/get/teacher/user/list')
            .then(val=>{   
                this.examiners = []
                this.examiners_copy = val.data        
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('honorschooltelst'));
                }                          
            })
        },
        
         /************************************************************
          * 
          * 加载学科组
          * 
          ************************************************************/ 
        loadep() {
            this.$http.get('api/user/v1/get/subject/department/list')
            .then(val=>{     
                this.trees = val.data.map(item=> ({
                    value: item.id,
                    title: item.department_name.zh?item.department_name.zh:item.department_name.mn
                }))     
            })
            .catch(err=>{
                if(err.code && err.code==422) {
                    this.$Message.info(this.$t('gaoyanxkz'));
                }                          
            })
        },
        toselec(tp) {
            let val=[]
            tp.forEach((item,index)=>{
                let ss=this.examiners.filter(item1 => item1.user_id == item)
                val[index]=ss[0]
            })
            this.$emit('on-select',val)
        }
    },
    created() {
        this.zwload()
        this.getExamine()
    }
}
</script>

<style>

</style>
